package com.cpedia.android.deal.activity;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.os.Bundle;
import android.os.Process;
import android.os.Environment;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.util.Config;
import android.util.Log;
import android.net.Uri;
import android.widget.Toast;
import com.cpedia.android.deal.*;

import java.util.Date;
import java.io.*;

/**
 * User: pchen
 * Date: Jun 5, 2009
 */
public class DealList extends Activity {
    private static final String EXTRA_ACCOUNT = "account";
    private static final String EXTRA_CLEAR_NOTIFICATION = "clearNotification";

    private Account mAccount;

    private Listener mListener = new Listener();
    private DealListViewHandler mHandler = new DealListViewHandler();

    class DealListViewHandler extends Handler {
        private static final int MSG_PROGRESS = 2;
        private static final int MSG_SET_HEADERS = 5;
        private static final int MSG_NETWORK_ERROR = 6;

        @Override
        public void handleMessage(android.os.Message msg) {
        }

        public void progress(boolean progress) {
            android.os.Message msg = new android.os.Message();
            msg.what = MSG_PROGRESS;
            msg.arg1 = progress ? 1 : 0;
            sendMessage(msg);
        }

        public void setHeaders(
                String subject,
                String from,
                String time,
                String date,
                String to,
                String cc,
                boolean hasAttachments) {
            android.os.Message msg = new android.os.Message();
            msg.what = MSG_SET_HEADERS;
            msg.arg1 = hasAttachments ? 1 : 0;
            msg.obj = new String[] { subject, from, time, date, to, cc };
            sendMessage(msg);
        }

        public void networkError() {
            sendEmptyMessage(MSG_NETWORK_ERROR);
        }
    }

    static class DealUpdateWorker implements Runnable {
        boolean mSynchronizeRemote;
        DealListener mListener;
        Account mAccount;
        DealController mController;

        public DealUpdateWorker(boolean synchronizeRemote,
                DealListener listener, Account account, DealController controller) {
            mSynchronizeRemote = synchronizeRemote;
            mListener = listener;
            mAccount = account;
            mController = controller;
        }

        public void run() {
            // Lower our priority
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            // Synchronously load the list of local messages
            mController.listLocalDeals(
                    mAccount,
                    mListener);
            if (mSynchronizeRemote) {
                // Tell the MessagingController to run a remote update of this folder
                // at it's leisure
                mController.synchronizeDealList(
                        mAccount,
                        mListener);
            }
        }
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.deallist);

        final String mimeType = "text/html";
        final String encoding = "utf-8";

        WebView wv;

        wv = (WebView) findViewById(R.id.wv1);
        wv.loadData("<a href='x'>Hello World! - 1</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv2);
        wv.loadData("<a href='x'>Hello World! - 2</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv3);
        wv.loadData("<a href='x'>Hello World! - 3</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv4);
        wv.loadData("<a href='x'>Hello World! - 4</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv5);
        wv.loadData("<a href='x'>Hello World! - 5</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv6);
        wv.loadData("<a href='x'>Hello World! - 6</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv7);
        wv.loadData("<a href='x'>Hello World! - 7</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv8);
        wv.loadData("<a href='x'>Hello World! - 8</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv9);
        wv.loadData("<a href='x'>Hello World! - 9</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv10);
        wv.loadData("<a href='x'>Hello World! - 10</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv11);
        wv.loadData("<a href='x'>Hello World! - 11</a>", mimeType, encoding);

        wv = (WebView) findViewById(R.id.wv12);
        wv.loadData("<a href='x'>Hello World! - 12</a>", mimeType, encoding);
        registerForContextMenu(wv);

        DealController.getInstance(getApplication()).addListener(mListener);

    }

    @Override
    public void onPause() {
        super.onPause();
        //DealController.getInstance(getApplication()).removeListener(mAdapter.mListener);
    }

    /**
     * On resume we refresh the folder list (in the background) and we refresh the messages
     * for any folder that is currently open. This guarantees that things like unread message
     * count and read status are updated.
     */
    @Override
    public void onResume() {
        super.onResume();

        NotificationManager notifMgr = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        notifMgr.cancel(1);

        //DealController.getInstance(getApplication()).addListener(mAdapter.mListener);
        mAccount.refresh(Preferences.getPreferences(this));
        onRefresh(false);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putParcelable(STATE_KEY_LIST, mListView.onSaveInstanceState());
        //outState.putInt(STATE_KEY_EXPANDED_GROUP, mExpandedGroup);
        //outState.putLong(STATE_KEY_EXPANDED_GROUP_SELECTION, mListView.getSelectedPosition());
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.refresh:
                onRefresh(true);
                return true;
            case R.id.search:
                //onSearch();
                return true;
            case R.id.account_settings:
                onEditAccount();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.deal_list_option, menu);
        return true;
    }


    private void onRefresh(final boolean forceRemote) {
        new Thread() {
            public void run() {
                android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
/*
                DealController.getInstance(getApplication()).listDeal(
                        mAccount,
                        forceRemote,
                        mAdapter.mListener);
*/
            }
        }.start();
    }


    private void onEditAccount() {
        PreferencesActivity.actionSettings(this, mAccount);
    }

    public static Intent actionHandleAccountIntent(Context context, Account account) {
        Intent intent = new Intent(context, DealList.class);
        intent.putExtra(EXTRA_ACCOUNT, account);
        intent.putExtra(EXTRA_CLEAR_NOTIFICATION, true);
        return intent;
    }

    class Listener extends DealListener {

    }


}
